<?php
$admin_logined  = false;

function checkAdminLogin()
{
    if (gpc_getSession("ad_login")==="admin_logined")
    {
        return true;
    }
    return false;
}


function Login($username,$password,$pass_md5=false)
{
global $dbsql,$language,$error_function;

	if ( empty($username)||empty($password) )
	{
	   $error_function = getLang("Please enter user name and password");
	   return false;
	}

	 //--------------------------------
	 $sql_select = "SELECT * FROM tblaccount WHERE  cName = '".$username."' or cEmail='$username'";
	 $rs=$dbsql->query($sql_select);
	 if ($dbsql->num_rows($rs)==0)
      {
           $error_function = getLang("Username or password not match");
		   return false;
      }

	  $result 		= $dbsql->fetch_array($rs);
      $pass      	= $result["cPassword"];
      $iAccountId 	= $result["iAccountId"];
      $cName 		= $result["cName"];
	  $groupId   	=  $result["iGroupId"];
	  $status		= $result['iStatus'];

	  //--------------------------------
	  if($status!=1 || $groupId ==0)
	  {
	  	   $error_function = "username : $username has been locked or not permission";
		   return false;
	  }

	   if (gpc_Md5($password)!=$pass && ($pass_md5===false))
      {
           $error_function = getLang("Password not corect");
		   return false;
      }

	  //======LOGIN============

	  gpc_setSession("ad_login","admin_logined");
	  gpc_setSession("AccountId",$iAccountId);
	  gpc_setSession("fullnameadmin",$cName);

	  $action = "<b><font color=red>Logined:  $cName </font></b>";
	  Insert_Tracking($action);
	  return true;
}

function getUserNamePost()
{
global $dbsql,$language;
    $accountid  = getIdAdmin();
	$sql_select="select cName from tblaccount where iAccountId=".$accountid;
	$result=$dbsql->query_fetch($sql_select);
	return $result['cName'];
}

function getIdAdmin()
{
    return intval(gpc_getSession("AccountId"));
}

function Logout()
{
	session_destroy();
	if(LOGIN_MOD==1)
	{
		header("location: http://logout:logout@".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
		exit;
	}
	header("Location: ?module=index&opt=login");
	exit();
}


function Checkuser($username)
{
global $dbsql,$language;

	$sql_select="select iAccountId from tblaccount where cName='$username'";
	$rs=$dbsql->query($sql_select);
	$rows=$dbsql->num_rows($rs);
	if($rows>0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function CheckEmail($email)
{
global $dbsql,$language;

	$sql_select="select iAccountId from tblaccount where cEmail='$email'";
	$rs=$dbsql->query($sql_select);
	$rows=$dbsql->num_rows($rs);
	if($rows>0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function getAccountById($id)
{
global $dbsql,$langauge;
	$sql_select="select * from tblaccount where iAccountId='$id'";
	$result=$dbsql->query_fetch($sql_select);
    $result['cFullName']    = trim($result['cFirstName'].' '.$result['cLastName']);
	return $result;
}

function getFullNameByAccountId($id)
{
	global $dbsql,$language;
	$sql_select="select cFirstName,cLastName from tblaccount where iAccountId=$id";
	$result=$dbsql->query_fetch($sql_select);
	$result=trim($result['cFirstName'].' '.$result['cLastName']);

	return $result;
}

function getInfoAccount($id)
{
global $dbsql,$language;
	$sql_select="select cName,cFirstName,cLastName,dBirthday,cEmail,cEmail2,cCompany,
			 cAddress,cAddress2,cAddress3,cCity,cState,cZipCode,cCountry,iGender,
			 cMobiPhone,cHomePhone,cWorkPhone,cFax,iStatus,dEntryDate,iGroupId,cInfo
			  from tblaccount where iAccountId=$id";
	$result=$dbsql->query_fetch($sql_select);
	$result['cFullName']=trim($result['cFirstName'].' '.$result['cLastName']);

	return $result;
}

function ListStatusUser($select)
{
	$status[1]="-1";
	$status[2]="0";
	$status[3]="1";

	$name[1]="Locked";
	$name[2]="InActive";
	$name[3]="Active";

	$option="";
	for($i=1;$i<=3;$i++)
	{
		if($status[$i]==$select)
		{
			$option.="<option value='".$status[$i]."' selected>".$name[$i]."</option>";
		}else
		{
			$option.="<option value='".$status[$i]."' >".$name[$i]."</option>";
		}
	}
	return $option;
}

function List_Group_Change($idselect=-10)
{
	global $dbsql;

	$sql_select="select iId,cGroupName from tblpermission order by iId";
	$rs=$dbsql->query($sql_select);
	$return="<option value='0' ".(($idselect==0)? "selected":"").">Customer</option>";
	while($result=$dbsql->fetch_array($rs))
	{
		if($idselect==$result['iId'])
		{
			$return.="<option value='".$result["iId"]."' selected='selected'>".$result["cGroupName"]."</option>";
		}else
		{
			$return.="<option value='".$result["iId"]."'>".$result["cGroupName"]."</option>";
		}
	}
	return $return;
}

?>